package server_V1;
// <editor-fold defaultstate="collapsed" desc="Imports">
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import lib.StreamSocket;
import serverMain.ServerCounter;
// </editor-fold> 

/**
 * @file Server.java
 *
 * @version 0.1
 * @author Chris Sewell 10008342
 */
public class ProtocallThreadV1 extends Thread {
    // <editor-fold defaultstate="collapsed" desc="Attributes">

    StreamSocket clientStream;
    String clientInput;
    ServerCounter downloadCounter;
    boolean runnable = true;

    // </editor-fold> 
    // <editor-fold defaultstate="collapsed" desc="Constructors">
    public ProtocallThreadV1(Socket acceptedSocket, ServerCounter downloadCounter) {
        try {
            clientStream = new StreamSocket(acceptedSocket);
        } catch (IOException ex) {
            Logger.getLogger(ProtocallThreadV1.class.getName()).log(Level.SEVERE, null, ex);
        }

        this.downloadCounter = downloadCounter;
    }
    // </editor-fold> 
    // <editor-fold defaultstate="collapsed" desc="Methods">

    //Terms of refrence input validator
    public boolean validateClientRefInput(String clientInput) {
        if ((clientInput.equalsIgnoreCase("Y"))
                || (clientInput.equalsIgnoreCase("N"))) {
            return true;
        } else {
            clientStream.sendString("Invalid input --- Terms of refrence. Do you accept? Y or N");
            return false;
        }
    }

    //File/Description input validator
    public boolean validateClientMenuInput(String clientInput) {
        if ((clientInput.equalsIgnoreCase("1"))
                || (clientInput.equalsIgnoreCase("2"))
                || (clientInput.equalsIgnoreCase("3"))) {
            return true;
        } else {
            clientStream.sendString("Invalid choice --- 1. computer program 2. picture 3. e-book");
            return false;
        }
    }

    //Another file/description input validator
    public boolean validateClientAnotherInput(String clientInput) {
        if ((clientInput.equalsIgnoreCase("Y"))
                || (clientInput.equalsIgnoreCase("N"))) {
            return true;
        } else {
            clientStream.sendString("Invalid input --- Another? Y or N");
            return false;
        }
    }

    @Override
    public void run() {
        System.out.print("New client connected, client " + clientStream.displayDetail() + " now connected\n");
        clientStream.sendString("Terms of refrence. Do you accept? Y or N");

        do { //Terms of service        
            clientInput = clientStream.recieveString();
        } while (!validateClientRefInput(clientInput));

        if (clientInput.equalsIgnoreCase("N")) {
            try {
                //Close connection if N
                clientStream.kill();
            } catch (IOException ex) {
                Logger.getLogger(ProtocallThreadV1.class.getName()).log(Level.SEVERE, null, ex);
            }

            //Client has disconnected
            System.out.print("Client " + clientStream.displayDetail() + " has disconnected\n");
            runnable = false;
        }

        while (runnable == true) {
            clientStream.sendString("1. computer program 2. picture 3. e-book");
            do { //Main menu
                clientInput = clientStream.recieveString();
            } while (!validateClientMenuInput(clientInput));

            int clientSelection = Integer.parseInt(clientInput);
            switch (clientSelection) {//Process choice
                case 1: {
                    clientStream.sendString("The program displays a message");
                    downloadCounter.incrementJavaCount();//Increment download counter
                    System.out.print(clientStream.displayDetail() + " has sucsessfully received the computer program\n");
                    //Print client information
                    System.out.print("Total downloads java: " + downloadCounter.getJavaCount() + "\n");
                }
                break;
                case 2: {
                    clientStream.sendString("This picture shows ...");
                    downloadCounter.incrementPictureCount();//Increment download counter
                    System.out.print(clientStream.displayDetail() + " has sucsessfully received the jpeg image\n");
                    //Print client information
                    System.out.print("Total downloads for picture: " + downloadCounter.getPictureCount() + "\n");
                }
                break;
                case 3: {
                    clientStream.sendString("The book is about ...");
                    downloadCounter.incrementBookCount();//Increment download counter
                    System.out.print(clientStream.displayDetail() + " has sucsessfully received the book\n");
                    //Print client information
                    System.out.print("Total downloads for book: " + downloadCounter.getBookCount() + "\n");
                }
                break;
            }
            clientStream.sendString("Another? Y or N");
            do { //Another file/description?    
                clientInput = clientStream.recieveString();
            } while (!validateClientAnotherInput(clientInput));

            if (clientInput.equalsIgnoreCase("N")) {
                try {
                    //Close connection if N
                    clientStream.kill();
                } catch (IOException ex) {
                    Logger.getLogger(ProtocallThreadV1.class.getName()).log(Level.SEVERE, null, ex);
                }
                //Client has disconnected
                System.out.print("Client " + clientStream.displayDetail() + " has disconnected\n");
                runnable = false;
            }
        }
    }
    // </editor-fold> 
}
